﻿using System;
using System.Windows.Forms;
using System.Linq;
using System.Data;
using System.Data.Entity;

namespace Runner
{
    public partial class LogView : Form
    {
        public LogView()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LogsSel();
        }

        private void LogView_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Value = DateTime.Now.Date.AddDays(-1);
            dateTimePicker2.Value = DateTime.Now;


            dateTimePicker4.Value = DateTime.Now.Date;
            dateTimePicker3.Value = DateTime.Now;

            LogsSel();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void LogsSel()
        {
            try
            {
                var log = new LogDBEntitiesRunner();
                try
                {
                    var dt1 = dateTimePicker1.Value.Date + dateTimePicker2.Value.TimeOfDay;
                    var dt2 = dateTimePicker4.Value.Date + dateTimePicker3.Value.TimeOfDay;
                    var query = from l in log.Logs
                                join h in log.LogHeaders on l.LogHeaderID equals h.LogHeaderID
                                join t in log.LogTypes on h.LogTypeID equals t.LogTypeID
                                where l.LogTime >= dt1 && l.LogTime <= dt2 && ((l.FileName.Contains(textBox1.Text) && textBox1.Text != "") || textBox1.Text == "")
                                orderby l.LogTime descending, l.LogID descending
                                select new LogsView()
                                {
                                    LogID = l.LogID,
                                    LogTypeName = t.LogType,
                                    LogTime = l.LogTime,
                                    LogHeaderName = h.LogHeaderName,
                                    ExceptionMessage = l.ExceptionMessage,
                                    FileName = l.FileName,
                                    FileSize = l.FileSize,
                                    CheckSum = l.CheckSum
                                };
                    dataGridView1.Rows.Clear();
                    foreach (LogsView lv in query)
                    {
                        var row = new DataGridViewRow();
                        row.CreateCells(dataGridView1);
                        row.Cells[0].Value = lv.LogID;
                        row.Cells[1].Value = lv.LogTypeName;
                        row.Cells[2].Value = lv.LogTime;
                        row.Cells[3].Value = lv.LogHeaderName;
                        row.Cells[4].Value = lv.ExceptionMessage;
                        row.Cells[5].Value = lv.FileName;
                        row.Cells[6].Value = lv.FileSize;
                        row.Cells[7].Value = lv.CheckSum;
                        dataGridView1.Rows.Add(row);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    if (log.Connection.State != ConnectionState.Closed)
                        log.Connection.Close();
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.IndexOf("Произошла ошибка базового поставщика в Open") > 0)
                    MessageBox.Show("В данный момент база данных лога занята другим процессом.\nПопробуйте еще раз");
                else MessageBox.Show(ex.Message);
            }
        }
    }

    public class LogsView
    {
        public long LogID { get; set; }
        public string LogTypeName { get; set; }
        public DateTime LogTime { get; set; }
        public string LogHeaderName { get; set; }
        public string ExceptionMessage { get; set; }
        public string FileName { get; set; }
        public long? FileSize { get; set; }
        public string CheckSum { get; set; }
    }
}
